package com.cola;

import org.junit.jupiter.api.Test;

import java.lang.reflect.*;

/**
 * @Grup:com.cola
 * @Auther:cola
 * @Data:2021/4/6-2021-04-19:13
 * @version:1.0
 *
 * 获取Class对象的三种方式
 */
public class ReflectionDemo {
    @Test
    public void test5(){
        Dog dog=new Dog("哇嘎",2,"白");
        Class<Dog> dogClass=Dog.class;
        Package aPackage = dogClass.getPackage();
        //System.out.println(aPackage.getName());
        //获取公共的方法,包括继承的共有方法
        Method[] methods = dogClass.getMethods();
        for (int i = 0; i <methods.length ; i++) {
            System.out.println(methods[i]);
            if (methods[i].getName().equals("toString")){
                try {
                    String s = (String)methods[i].invoke(dog);
                    System.out.println(s);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("-----------------------------------------------------------");
        //访问室私有方法 ,获取到本类中定义的所有方法(不包括父类)
        Method[] declaredMethods = dogClass.getDeclaredMethods();
        for (int i = 0; i <declaredMethods.length ; i++) {
            System.out.println(declaredMethods[i]);
            if (declaredMethods[i].getName().equals("set")){
                //设置私有方法可以被访问(去除访问修饰符的检查)
                declaredMethods[i].setAccessible(true);
                try {
                    declaredMethods[i].invoke(dog);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test4(){
        Class<Dog> dogClass=Dog.class;
        //获取非私有属性
        Field[] fields=dogClass.getFields();
        System.out.println(fields.length);
        //获取所有属性(包括私有属性)
        Field[] declaredFields = dogClass.getDeclaredFields();
        System.out.println(declaredFields.length);
        for (int i = 0; i <declaredFields.length ; i++) {
            System.out.println(Modifier.toString(declaredFields[i].getModifiers())+" :"+declaredFields[i].getName()+":"+declaredFields[i].getType());
        }
    }

    @Test
    public void test3(){
        Class<Dog> dogClass=Dog.class;
        Constructor<?>[]constructors=dogClass.getConstructors();
        for (int i = 0; i <constructors.length ; i++) {
            System.out.println(constructors[i].getName());
            System.out.println(constructors[i].getParameterCount());
        }

        //调用带参数的构造器来实例化对象
        //方法一
        try {
            Constructor<Dog>constructor= dogClass.getConstructor(String.class,int.class,String.class);
            Dog dog1 = constructor.newInstance("小白", 5, "白");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //方法二 JDK1.9之后
        try {
            Dog dog2 =(Dog)dogClass.getDeclaredConstructor(String.class,int.class,String.class).newInstance("小胖",6,"黄");
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }


    }
    /*
    * 通过反射来实例化对象
    * */
    @Test
    public void test2(){
        Class<Dog> dogClass=Dog.class;
        try {
            //通过Class对象实例化对象,调用无参的构造方法
            //方法一 已过时被弃用
            //Dog dog = (Dog) dogClass.newInstance();
            //方法二 JDK1.9之后
            Dog dog =(Dog)dogClass.getDeclaredConstructor().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test1(){
        //通过对象的getClass()方法
        Dog dog =new Dog("泰迪",4,"白");
        Class aClass = dog.getClass();
        //通过类.class
        Class dogClass = Dog.class;

        //通过class.forName方法
        try {
            Class aClass1 = Class.forName("com.cola.Dog");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
